package cn.acac.yygh.order.controller;

import cn.acac.yygh.enums.OrderStatusEnum;
import cn.acac.yygh.model.order.OrderInfo;
import cn.acac.yygh.order.service.OrderInfoService;
import cn.acac.yygh.result.R;
import cn.acac.yygh.utils.JwtHelper;
import cn.acac.yygh.vo.order.OrderQueryVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user/order/orderInfo")
public class OrderController {

    @Autowired
    private OrderInfoService orderInfoService;


    /**
     * 取消预约的接口
     * @param orderId
     * @return
     */
    @GetMapping("/cancelOrder/{orderId}")
    public R cancelOrder(@PathVariable Long orderId){
        orderInfoService.cancelOrder(orderId);
        return R.ok();

    }

    /**
     * 根据订单id查询订单信息
     * @param orderId
     * @return
     */
    @GetMapping("/{orderId}")
    public R findOrderInfoByOrderId(@PathVariable Long orderId){
        OrderInfo orderInfo = orderInfoService.findOrderInfoByOrderId(orderId);
        return R.ok().setData("orderInfo",orderInfo);
    }

    /**
     * 根据排班id和就诊人id生成订单信息
     * @param scheduleId
     * @param patientId
     * @return
     */
    @PostMapping("/{scheduleId}/{patientId}")
    public R submitOrder(@PathVariable String scheduleId,
                         @PathVariable Long patientId){
        Long orderId = orderInfoService.submitOrder(scheduleId,patientId);
        return R.ok().setData("orderId",orderId);
    }


    /**
     * 待条件的分页查询订单接口
     * @param pageNum
     * @param pageSize
     * @param orderQueryVo
     * @return
     */
    @GetMapping("/{pageNum}/{pageSize}")
    public R selectOrderPage(@PathVariable Integer pageNum,
                             @PathVariable Integer pageSize,
                             OrderQueryVo orderQueryVo,
                             @RequestHeader String token){
        // 获取当前登录的用户id
        Long userId = JwtHelper.getUserId(token);
        orderQueryVo.setUserId(userId);
        Page<OrderInfo> page = orderInfoService.selectOrderPage(pageNum,pageSize,orderQueryVo);
        return R.ok().setData("page",page);
    }

    /**
     * 获取所有的状态信息
     * @return
     */
    @GetMapping("/getStatusList")
    public R getStatusList(){
        List<Map<String, Object>> statusList = OrderStatusEnum.getStatusList();
        return R.ok().setData("statusList",statusList);
    }

}
